package com.ysstech.valuation.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.util.DateUtil;
import com.ysstech.valuation.entity.Valuation;
import com.ysstech.valuation.entity.ValuationTotal;
import com.ysstech.valuation.mapper.ValuationTotalMapper;
import com.ysstech.valuation.service.ValuationTotalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-05-13
 */
@Service
public class ValuationTotalServiceImpl extends ServiceImpl<ValuationTotalMapper, ValuationTotal> implements ValuationTotalService {


    /**
     * 处理估值表合计项
     *
     * @param listadd   明细记录
     * @param valuation 页面选择参数
     * @throws Exception
     */
    public void insertValuationTotal(List<Valuation> listadd, Valuation valuation) throws Exception {
        if (CollectionUtils.isEmpty(listadd) || null == valuation) {
            return;
        }
        QueryWrapper<ValuationTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("FDATE", DateUtil.strToLocalDate(valuation.getSelfdate()));
        wrapper.eq("ACCOUNT_ID", valuation.getAccountId());
        this.remove(wrapper);
        this.insertValuationTotal101(listadd, valuation);
    }

    /**
     * @param listadd
     * @param valuation
     * @throws Exception
     */
    public void insertValuationTotal101(List<Valuation> listadd, Valuation valuation) throws Exception {
        if (CollectionUtils.isEmpty(listadd)) {
            return;
        }
        // 净值的成本和市值  成本=市值-估值增值 市值=资产类+共同类-负债类
        BigDecimal equityCost = BigDecimal.ZERO;
        BigDecimal equityMarket = BigDecimal.ZERO;
        // 资产类成本和市值 成本=资产类+共同类 市值=资产类+共同类
        BigDecimal assetsCost = BigDecimal.ZERO;
        BigDecimal assetsMarket = BigDecimal.ZERO;
        // 负债类的成本和市值
        BigDecimal liabilitiesCost = BigDecimal.ZERO;
        BigDecimal liabilitiesMarket = BigDecimal.ZERO;
        for (Valuation v : listadd) {
            if (null == v || null == v.getAssetsType()) {
                continue;
            }
            //资产类 共同类
            if (1 == v.getAssetsType() || 3 == v.getAssetsType()) {
                equityCost = equityCost.add(v.getCost());
                equityMarket = equityMarket.add(v.getMarketValue());
                assetsCost = assetsCost.add(v.getCost());
                assetsMarket = assetsMarket.add(v.getMarketValue());
            }
            // 负债类
            if (2 == v.getAssetsType()) {
                equityCost = equityCost.subtract(v.getCost());
                equityMarket = equityMarket.subtract(v.getMarketValue());
                liabilitiesCost = liabilitiesCost.add(v.getCost());
                liabilitiesMarket = liabilitiesMarket.add(v.getMarketValue());
            }
        }
        List<ValuationTotal> list = new ArrayList<>();
        // 资产净值
        list.add(getValuationTotal(valuation, equityCost, equityMarket, "101"));
        // 资产类合计
        list.add(getValuationTotal(valuation, assetsCost, assetsMarket, "102"));
        // 负债类合计
        list.add(getValuationTotal(valuation, liabilitiesCost, liabilitiesMarket, "103"));
        this.saveBatch(list);
    }

    private ValuationTotal getValuationTotal(Valuation valuation, BigDecimal cost, BigDecimal marketValue, String type) throws Exception {
        ValuationTotal valuationTotal = new ValuationTotal();
        valuationTotal.setId(DateUtil.generateUUID());
        valuationTotal.setAccountId(valuation.getAccountId());
        valuationTotal.setFdate(DateUtil.strToLocalDate(valuation.getSelfdate()));
        valuationTotal.setCost(cost);
        valuationTotal.setMarketValue(marketValue);
        valuationTotal.setType(type);
        valuationTotal.setAddTime(LocalDateTime.now());
        return valuationTotal;
    }


}
